import numpy as np
from DATA import DATA
class CAR(object):
    def __init__(self,id_,from_,to_,speed_,planTime_,route):
        # **** statistic parameters ****#
        self.id_, self.from_, self.to_, self.speed_, self.planTime_ ,self.route= id_, from_, to_, speed_, -1,route
        
        # **** dynamic parameters ****#
        self.state,self.x,self.y = 0,0,0
        self.presentRoad, self.nextCrossId = None,self.from_
        self.wait = False
        self.routeIndex = None
    #
    # simulate initialization
    #
    def simulateInit(self,planTime):
        self.planTime_,self.routeIndex = planTime,0
    #
    # dynamic param update
    #
    """
    
        updateDynamic 更新车辆的状态
        仅有 state=1 车辆状态为可以前进
        car state: 0,1,2,3 in 在车库,等待,完成一步行走,行程结束
        self.x = x 表示在道路的坐标
    
    
    """
    def updateDynamic(self,state,x=None,y=None,presentRoad=None,roadSpeed=None,nextCrossId=None):
        # car not in carport of car is ready to go
        if self.state != 0 or presentRoad is not None:
            self.state = state
        if presentRoad is not None and self.state != 0 and self.routeIndex < self.route.__len__():
            self.routeIndex += 1
        self.x = x if x is not None else self.x
        self.y = y if y is not None else self.y
        self.presentRoad = presentRoad if presentRoad is not None else self.presentRoad
        if nextCrossId is not None:
            self.nextCrossId = nextCrossId
            toX, toY = DATA.CROSSDICT[self.to_].__loc__()
            nextCrossX, nextCrossY = DATA.CROSSDICT[nextCrossId].__loc__()
            self.deltaX, self.deltaY = toX - nextCrossX, toY - nextCrossY
    # show statistic parameters
    def __id__(self):
        return self.id_
    def __from__(self):
        return self.from_
    def __to__(self):
        return self.to_
    def __speed__(self):
        return  self.speed_
    def __planTime__(self):
        return self.planTime_

    #
    # show dynamic parameters
    #
    def __state__(self):
        return self.state
    def __x__(self):
        return self.x
    def __y__(self):
        return self.y
    def __presentRoad__(self):
        return self.presentRoad
    def __nextCrossId__(self):
        return self.nextCrossId
    def __wait__(self):
        return self.wait
    def __route__(self):
        return self.route
    def __routeIndex__(self):
        return self.routeIndex
    #
    # show some important info
    #
    def __v__(self):
        return min(self.speed_,DATA.ROADDICT[self.presentRoad].__speed__())
    def __nextRoad__(self):
        try:
            return self.route[self.routeIndex]
        except:
            return -1